package com.mx.zb.mng.ctrl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.mx.zb.mng.dao.AgentLevelMapper;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.utils.RestInter;
import com.mx.zb.mng.utils.context.JsonConfig;

/**
 * 
 * @ClassName: AgentLevelRestCtrl
 * @Description: 代理商级别
 * @author 李志伟
 * @date 2017年9月25日 下午10:11:12 
 *
 */
@Controller
@RequestMapping("p/calc")
public class LogManageCtrl {
	

	@Autowired
	AgentLevelMapper agentLevelMapper;
	
	@Autowired
	AgentMapper agentMapper;
	
	@Autowired
	RestInter restInter;
	@Value("${custom.tenant.host}")
	private String tenantHost;

	@Value("${custom.tenant.jsonConfigPath}")
	private String jsonConfigPath;
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	
	@RequestMapping(value= {"/mng"}, method= {RequestMethod.GET})
	public String toAgent(Model model) {
		JSONObject o=JsonConfig.getJson();
		List<String> list=getJsonData(o);
		 model.addAttribute("list", list);
		return "platformLog";
	}
	
	@RequestMapping(value= {"/tenant"}, method= {RequestMethod.GET})
	public String toTenant(Model model) {
		  JSONObject json= restInter.postJson( tenantHost + jsonConfigPath, new JSONObject());
		List<String> list=getJsonData(json.getJSONObject("data"));
		 model.addAttribute("list", list);
		return "tenantLog";
	}
	
	public List<String> getJsonData(JSONObject o) {
		 Set<String> keys = o.keySet();
		 List<String> listTmp=new ArrayList<String>();
		 for (String key : keys) {  
		      System.out.println(o.get(key));  
		      listTmp.add(o.get(key).toString());
		}
		 List<String> list=new ArrayList<String>();
		 for (String tmpStr : listTmp) {  
			 JSONObject obj=  JSONObject.parseObject(tmpStr);
			 Set<String> types = obj.keySet();
			 for (String type : types) {  
			      System.out.println(o.get(type));  
			      list.add(obj.get(type).toString());
			}
		}
		 return list;
	}
	
}
